package cn.iocoder.yudao.module.bill.controller.admin.tag;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.bill.controller.admin.tag.vo.*;
import cn.iocoder.yudao.module.bill.dal.dataobject.tag.BillTagDO;
import cn.iocoder.yudao.module.bill.service.tag.BillTagService;

@Tag(name = "管理后台 - 账单标签")
@RestController
@RequestMapping("/bill/tag")
@Validated
public class BillTagController {

    @Resource
    private BillTagService tagService;

    @PostMapping("/create")
    @Operation(summary = "创建账单标签")
    @PreAuthorize("@ss.hasPermission('bill:tag:create')")
    public CommonResult<Long> createTag(@Valid @RequestBody BillTagSaveReqVO createReqVO) {
        return success(tagService.createTag(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新账单标签")
    @PreAuthorize("@ss.hasPermission('bill:tag:update')")
    public CommonResult<Boolean> updateTag(@Valid @RequestBody BillTagSaveReqVO updateReqVO) {
        tagService.updateTag(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除账单标签")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bill:tag:delete')")
    public CommonResult<Boolean> deleteTag(@RequestParam("id") Long id) {
        tagService.deleteTag(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得账单标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bill:tag:query')")
    public CommonResult<BillTagRespVO> getTag(@RequestParam("id") Long id) {
        BillTagDO tag = tagService.getTag(id);
        return success(BeanUtils.toBean(tag, BillTagRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得账单标签分页")
    @PreAuthorize("@ss.hasPermission('bill:tag:query')")
    public CommonResult<PageResult<BillTagRespVO>> getTagPage(@Valid BillTagPageReqVO pageReqVO) {
        PageResult<BillTagDO> pageResult = tagService.getTagPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, BillTagRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出账单标签 Excel")
    @PreAuthorize("@ss.hasPermission('bill:tag:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportTagExcel(@Valid BillTagPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BillTagDO> list = tagService.getTagPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "账单标签.xls", "数据", BillTagRespVO.class,
                        BeanUtils.toBean(list, BillTagRespVO.class));
    }

}